﻿using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DeepZoomPivotConstructor.VisualLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RenderEngine;
using RenderEngineInterfaces;
using SimpleFileRendering;

namespace t_SimpleFileRendering
{


    /// <summary>
    ///This is a test class for TestWebShortCutRender and is intended
    ///to contain all TestWebShortCutRender Unit Tests
    ///</summary>
    [TestClass()]
    public class TestWebShortCutRender
    {
        [TestInitialize]
        public void InitForTest()
        {
            t_TestingLibrary.Utilities.MyClassInit();
            t_TestingLibrary.Utilities.AddPart(new RenderControl());
            t_TestingLibrary.Utilities.AddPart(new LayoutHelper());
            t_TestingLibrary.Utilities.AddPart(new MyOneRenderGuy());
            t_TestingLibrary.Utilities.AddPart(this);
        }

        [TestCleanup]
        public void ShutDownTest()
        {
            t_TestingLibrary.Utilities.MyClassDone();
        }


        /// <summary>
        ///A test for RenderPriority
        ///</summary>
        [TestMethod()]
        [DeploymentItem("cern.ch.website")]
        public void TestRenderPriority()
        {
            var target = new WebShortCutRender();
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, target.RenderPriority(new FileInfo("junk.website")), "wesite file doesn't exist");
            Assert.AreEqual(RenderPluginPriority.RPPGeneric, target.RenderPriority(new FileInfo("cern.ch.website")), "wesite file exist");

            using (var f = File.CreateText("junk.txt"))
            {
                f.WriteLine("hi");
                f.Close();
            }

            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, target.RenderPriority(new FileInfo("junk.txt")), "txt file exist");
        }

        /// <summary>
        ///A test for RenderPriority
        ///</summary>
        [TestMethod()]
        [DeploymentItem("cern.ch.url")]
        public void TestRenderPriorityURLFile()
        {
            var target = new WebShortCutRender();
            Assert.AreEqual(RenderPluginPriority.RPPGeneric, target.RenderPriority(new FileInfo("cern.ch.url")), "wesite file exist");
        }

        [TestMethod]
        [DeploymentItem("cern.ch.website")]
        public void TestRenderItem()
        {
            var target = new WebShortCutRender();
            t_TestingLibrary.Utilities.Compose(target);

            var v = target.Render(new FileInfo("cern.ch.website"));
            Assert.IsNotNull(v, "render item null");
            var r = v.Result;
            Assert.IsNotNull(r, "render item result is null");
            Assert.AreEqual(1, r.SGNodeList.Count(), "# of images");
            Assert.AreEqual("http://cdsweb.cern.ch/record/1368452/files/ATL-COM-PHYS-2011-956.pdf", MyOneRenderGuy.RenderedUri, "bad url found");
        }

        [TestMethod]
        [DeploymentItem("cern.ch.url")]
        public void TestRenderItemUrl()
        {
            var target = new WebShortCutRender();
            t_TestingLibrary.Utilities.Compose(target);

            var v = target.Render(new FileInfo("cern.ch.url"));
            Assert.IsNotNull(v, "render item null");
            var r = v.Result;
            Assert.IsNotNull(r, "render item result is null");
            Assert.AreEqual(1, r.SGNodeList.Count(), "# of images");
            Assert.AreEqual("https://indico.cern.ch/contributionDisplay.py?sessionId=0&contribId=116&confId=93877", MyOneRenderGuy.RenderedUri, "bad url found");
        }

        [TestMethod]
        [DeploymentItem("cern.ch.website")]
        public void TestRenderItemNoRendering()
        {
            var target = new WebShortCutRender();
            t_TestingLibrary.Utilities.Compose(target);
            MyOneRenderGuy.FailPriority = true;

            var v = target.Render(new FileInfo("cern.ch.website"));
            Assert.IsNull(v, "render item null");
        }

        [TestMethod]
        [DeploymentItem("cern.ch.website")]
        public void TestRenderItemFailedRender()
        {
            var target = new WebShortCutRender();
            t_TestingLibrary.Utilities.Compose(target);
            MyOneRenderGuy.FailDuringActualRender = true;

            var v = target.Render(new FileInfo("cern.ch.website"));
            Assert.IsNotNull(v, "render item task");
            Assert.IsNull(v.Result, "Render item task result");
        }

        [Import]
        public IRenderEngine MyRender { get; set; }

        [Export(typeof(IRenderItem))]
        [RenderInfo(typeof(Uri))]
        class MyOneRenderGuy : IRenderItem
        {
            public static string RenderedUri { get; set; }
            public static bool FailDuringActualRender { get; set; }
            public static bool FailPriority { get; set; }
            public MyOneRenderGuy()
            {
                RenderedUri = null;
                FailPriority = false;
                FailDuringActualRender = false;
            }
            public System.Threading.Tasks.Task<Visual> Render(object what)
            {
                RenderedUri = (what as Uri).OriginalString;
                return Task<Visual>.Factory.StartNew(() =>
                {
                    if (FailDuringActualRender)
                        return null;
                    return new VisualBitmapImage(1.0, 1.0, 300, System.Drawing.Color.Black);
                });
            }

            public RenderPluginPriority RenderPriority(object what)
            {
                if (FailPriority)
                    return RenderPluginPriority.RPPCantDoIt;
                return RenderPluginPriority.RPPGeneric;
            }
        }

        [TestMethod]
        [DeploymentItem("cern.ch.website")]
        public void TestMetaData()
        {
            t_TestingLibrary.Utilities.Compose(new WebShortCutRender());
            Visual v = MyRender.Render(new FileInfo("cern.ch.website"));

            Assert.IsNotNull(v, "Visual rendering of file should not have been null!");
            Assert.AreEqual(1, v.SGNodeList.Count, "Expected just one image to come back!");
        }
    }
}
